#region Code by Telligent Systems

//Last Updated: Robert McLaws
//Date: 22 Sept 2007

#endregion

#region Import Declarations

using System;
using System.Collections.Generic;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using CommunityServer.Components;
using CommunityServer.Controls;

#endregion

namespace CommunityServerStuff.Controls
{
    /// <summary>
    /// Renders a user's avatar.
    /// </summary>
    public class UserAvatar : WrappedContentBase
    {
        protected override void BindDefaultContent(System.Web.UI.Control control, System.Web.UI.IDataItemContainer dataItemContainer)
        {
            CSContext csContext = CSControlUtility.Instance().GetCurrentCSContext(this.Page);
            User user = this.DataSource as User;

            if (csContext.SiteSettings.EnableAvatars 
                && user != null 
                && csContext.User.EnableUserAvatars 
                && (ShowAnonymousAvatar || (user.HasAvatar && user.EnableAvatar && user.IsAvatarApproved)))
            {
                CommunityServer.Controls.Image image = new CommunityServer.Controls.Image();

                if (!Globals.IsNullorEmpty(this.ImageCssClass))
                    image.CssClass = this.ImageCssClass;

                if (!Globals.IsNullorEmpty(this.ImageId))
                    image.Attributes.Add("id", this.ImageId);

                if (user.HasAvatar && user.EnableAvatar && user.IsAvatarApproved)
                {
                    if (!user.AvatarUrl.StartsWith("users/avatar.aspx"))
                        image.ImageUrl = Globals.HtmlDecode(user.AvatarUrl);
                    else
                        image.ImageUrl = Globals.ApplicationPath + "/" + Globals.HtmlDecode(user.AvatarUrl);
                }
                else
                    image.ImageUrl = this.Page.Response.ApplyAppPathModifier(AnonymousAvatarUrl);

                if (this.Page.Request.Browser.IsBrowser("IE") && this.Page.Request.Browser.MajorVersion < 7)
                {
                    if (this.Height > this.Width)
                        image.Height = this.Height;
                    else
                        image.Width = this.Width;
                }
                else
                {
                    image.Style.Add("max-height", this.Height.ToString() + "px");
                    image.Style.Add("max-width", this.Width.ToString() + "px");
                }

                image.BorderWidth = this.BorderWidth;

                control.Controls.Add(image);
            }
            else
                this.AutomatedVisible = false;
        }

        private User _user = null;
        public override object DataSource
        {
            get
            {
                if (_user == null)
                {
                    if (this.UseAccessingUser)
                        _user = CSControlUtility.Instance().GetCurrentCSContext(this.Page).User;
                    else
                        _user = CSControlUtility.Instance().GetCurrentUser(this);
                }

                return _user;
            }
            set
            {
                if (value is User)
                    _user = value as User;
                else
                    throw new InvalidCastException("DataSource must be a valid User object");
            }
        }

        #region Properties

        /// <summary>
        /// When set, specifies the CSS class for the image control for the Avatar
        /// </summary>
        public string ImageCssClass
        {
            get { return (string)(ViewState["ImageCssClass"] ?? String.Empty); }
            set { ViewState["ImageCssClass"] = value; }
        }

        /// <summary>
        /// When set, specifies the ID for the image control for the Avatar
        /// </summary>
        public string ImageId
        {
            get { return (string)(ViewState["ImageId"] ?? String.Empty); }
            set { ViewState["ImageId"] = value; }
        }

        /// <summary>
        /// When true, the current user accessing the page will be used for processing instead of the current contextual user
        /// </summary>
        public bool UseAccessingUser
        {
            get { return (bool)(ViewState["UseAccessingUser"] ?? false); }
            set { ViewState["UseAccessingUser"] = value; }
        }

        /// <summary>
        /// Gets or sets a value indicating whether to show the anonymous user avatar.
        /// </summary>
        /// <value><c>true</c> if the anonymous user avatar will be shown; otherwise, <c>false</c>.</value>
        [System.ComponentModel.DefaultValue(true)]
        public virtual Boolean ShowAnonymousAvatar
        {
            get{ return (bool)(ViewState["ShowAnonymousAvatar"] ?? true); }
            set { ViewState["ShowAnonymousAvatar"] = value; }
        }

        /// <summary>
        /// Gets or sets the anonymous avatar URL.
        /// </summary>
        /// <value>The anonymous avatar URL.</value>
        [System.ComponentModel.DefaultValue("~/utility/anonymous.gif")]
        public virtual string AnonymousAvatarUrl
        {
            get { return (string)ViewState["AnonymousAvatarUrl"] ?? "~/utility/anonymous.gif"; }
            set { ViewState["AnonymousAvatarUrl"] = value; }
        }

        /// <summary>
        /// Gets or sets the width of the border.
        /// </summary>
        /// <value>The width of the border.</value>
        [System.ComponentModel.DefaultValue(0)]
        public virtual int BorderWidth
        {
            get { return (int)(ViewState["BorderWidth"] ?? 0); }
            set { ViewState["BorderWidth"] = value; }
        }
        /// <summary>
        /// If set this value overrides the default avatar sizing
        /// </summary>
        /// <value>The height.</value>
        public virtual int Height
        {
            get { return (int)(ViewState["Height"] ?? CSControlUtility.Instance().GetCurrentCSContext(this.Page).SiteSettings.AvatarHeight);}
            set { ViewState["Height"] = value; }
        }
        /// <summary>
        /// If set this value overrides the default avatar sizing
        /// </summary>
        /// <value>The width.</value>
        public virtual int Width
        {
            get { return (int)(ViewState["Width"] ?? CSControlUtility.Instance().GetCurrentCSContext(this.Page).SiteSettings.AvatarWidth); }
            set { ViewState["Width"] = value; }
        }

        #endregion
    }
}
